package qeb.handheldmedical.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import qeb.handheldmedical.R;

public class MultiDialog {
	// 主窗口view
	private Context context;
	// 退出函数回调接口事件
	private String message;

	private int dialogType = 1;

	private String title = "系统提醒";

	private Dialog dialog = null;

	private Button btnCancel;

	private Button btnGo;
	private Button btnKnow;
	private Button btn1, btn2, btn3, btn4, btn_psqd, btn_psqx;
	private String btn1Text, btn2Text, btn3Text, btn4Text;
	private String btnGoText = "确定";
	private String btnCancelText = "取消";
	private String strPsjg = "", strPsgh = "";
	private OnClickListener btnCancel_onClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			dialog.cancel();
		}
	};

	private OnClickListener btnKnow_onClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			dialog.cancel();
		}
	};

	private OnClickListener btnGo_onClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			dialog.cancel();
		}
	};

	private OnClickListener btn_psqd_onClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			dialog.cancel();
		}
	};
	private OnClickListener btn_psqx_onClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			dialog.cancel();
		}
	};

	public MultiDialog(Context context, int dialogType) {
		this.context = context;
		this.dialogType = dialogType;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setPsjg(String psgh, String psjg) {
		this.strPsgh = psgh;
		this.strPsjg = psjg;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public void setGoButtonText(String goButtonText) {
		this.btnGoText = goButtonText;
	}

	public void setbtnCancelText(String btnCancelText) {
		this.btnCancelText = btnCancelText;
	}

	public void setDialogGoListener(OnClickListener onClickListener) {
		this.btnGo_onClickListener = onClickListener;
	}

	public void setDialogCancelListener(OnClickListener onClickListener) {
		this.btnCancel_onClickListener = onClickListener;
		this.btnKnow_onClickListener = onClickListener;
	}

	public void setDialogKnowListener(OnClickListener onClickListener) {
		this.btnKnow_onClickListener = onClickListener;
	}

	public void setDialogKPsqdListener(OnClickListener onClickListener) {
		this.btn_psqd_onClickListener = onClickListener;
	}

	public void setDialogkPsqxListener(OnClickListener onClickListener) {
		this.btn_psqx_onClickListener = onClickListener;
	}

	public void setbtn1Text(String ButtonText) {
		this.btn1Text = ButtonText;
	}

	public void setbtn2Text(String ButtonText) {
		this.btn2Text = ButtonText;
	}

	public void setbtn3Text(String ButtonText) {
		this.btn3Text = ButtonText;
	}

	public void setbtn4Text(String ButtonText) {
		this.btn4Text = ButtonText;
	}

	public void setBtn1Listener(OnClickListener onClickListener) {
		this.btn1_onClickListener = onClickListener;
	}

	public void setBtn2Listener(OnClickListener onClickListener) {
		this.btn2_onClickListener = onClickListener;
	}

	public void setBtn3Listener(OnClickListener onClickListener) {
		this.btn3_onClickListener = onClickListener;
	}

	public void setBtn4Listener(OnClickListener onClickListener) {
		this.btn4_onClickListener = onClickListener;
	}

	private OnClickListener btn1_onClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			dialog.cancel();
		}
	};

	private OnClickListener btn2_onClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			dialog.cancel();
		}
	};
	private OnClickListener btn3_onClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			dialog.cancel();
		}
	};
	private OnClickListener btn4_onClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			dialog.cancel();
		}
	};

	private void AlertDialog() {

		dialog = new Dialog(context, R.style.dialog);
		LayoutInflater inflater = LayoutInflater.from(context);
		View layout = inflater.inflate(R.layout.multi_dialog, null);
		dialog.setContentView(layout);

		TextView txtdialog_title = (TextView) layout
				.findViewById(R.id.txtdialog_title);
		txtdialog_title.setText(title);
		TextView txtdialogMessage = (TextView) layout
				.findViewById(R.id.txtdialogMessage);
		txtdialogMessage.setText(message);
		btnKnow = (Button) layout.findViewById(R.id.btn_queding2);
		btnKnow.setText("知道了");
		btnKnow.setOnClickListener(btnKnow_onClickListener);
		TableRow table_confirm = (TableRow) layout
				.findViewById(R.id.table_confirm);
		TableRow table_alert = (TableRow) layout.findViewById(R.id.table_alert);

		TableRow table_FourButton = (TableRow) layout
				.findViewById(R.id.table_FourButton);

		TableRow table_alertPs = (TableRow) layout
				.findViewById(R.id.table_alertPs);

		table_confirm.setVisibility(TableRow.GONE);
		table_alert.setVisibility(TableRow.VISIBLE);
		table_FourButton.setVisibility(View.GONE);
		table_alertPs.setVisibility(View.GONE);
		dialog.show();
	}

	/*
	 * 
	 * 皮试结果输入
	 */
	public void PsjgDialog() {
		dialog = new Dialog(context, R.style.dialog);
		LayoutInflater inflater = LayoutInflater.from(context);
		View layout = inflater.inflate(R.layout.multi_dialog, null);
		dialog.setContentView(layout);

		TableRow table_alertPs = (TableRow) layout
				.findViewById(R.id.table_alertPs);

		TextView txtdialog_title = (TextView) layout
				.findViewById(R.id.txtdialog_title);
		txtdialog_title.setText(title);
		TextView txtdialogMessage = (TextView) layout
				.findViewById(R.id.txtdialogMessage);
		txtdialogMessage.setText(message);

		btn_psqd = (Button) layout.findViewById(R.id.btn_psqd);
		btn_psqd.setText("确定");
		btn_psqd.setOnClickListener(btn_psqd_onClickListener);

		btn_psqx = (Button) layout.findViewById(R.id.btn_psqx);
		btn_psqx.setText("取消");
		btn_psqx.setOnClickListener(btn_psqx_onClickListener);

		TableRow table_confirm = (TableRow) layout
				.findViewById(R.id.table_confirm);
		TableRow table_alert = (TableRow) layout.findViewById(R.id.table_alert);

		TableRow table_FourButton = (TableRow) layout
				.findViewById(R.id.table_FourButton);

		table_confirm.setVisibility(TableRow.GONE);
		table_alert.setVisibility(TableRow.GONE);
		table_FourButton.setVisibility(View.GONE);
		table_alertPs.setVisibility(TableRow.VISIBLE);

		EditText edtPsgh = (EditText) layout.findViewById(R.id.edtPsgh);

		edtPsgh.setText(strPsgh);
		RadioButton rbtPsjg1 = (RadioButton) layout.findViewById(R.id.rbtPsjg1);
		RadioButton rbtPsjg2 = (RadioButton) layout.findViewById(R.id.rbtPsjg2);
		if (strPsjg.equals("-")) {
			rbtPsjg1.setChecked(true);
		} else if (strPsjg.equals("+")) {
			rbtPsjg2.setChecked(true);
		}

		dialog.show();
	}

	public void ConfirmDialog() {
		dialog = new Dialog(context, R.style.dialog);
		LayoutInflater inflater = LayoutInflater.from(context);
		View layout = inflater.inflate(R.layout.multi_dialog, null);
		dialog.setContentView(layout);
		TextView txtdialog_title = (TextView) layout
				.findViewById(R.id.txtdialog_title);
		txtdialog_title.setText(title);
		TextView txtdialogMessage = (TextView) layout
				.findViewById(R.id.txtdialogMessage);
		txtdialogMessage.setText(message);
		btnGo = (Button) layout.findViewById(R.id.btn_queding);
		btnGo.setText(btnGoText);
		btnGo.setOnClickListener(btnGo_onClickListener);
		btnCancel = (Button) layout.findViewById(R.id.btn_quxiao);
		btnCancel.setText(btnCancelText);
		btnCancel.setOnClickListener(btnCancel_onClickListener);
		TableRow table_confirm = (TableRow) layout
				.findViewById(R.id.table_confirm);
		TableRow table_alert = (TableRow) layout.findViewById(R.id.table_alert);
		TableRow table_FourButton = (TableRow) layout
				.findViewById(R.id.table_FourButton);
		TableRow table_alertPs = (TableRow) layout
				.findViewById(R.id.table_alertPs);

		table_FourButton.setVisibility(View.GONE);
		table_confirm.setVisibility(TableRow.VISIBLE);
		table_alert.setVisibility(TableRow.GONE);
		table_alertPs.setVisibility(TableRow.GONE);
		dialog.show();
	}

	// 口服医嘱确认
	public void FourButtonDialog() {
		dialog = new Dialog(context, R.style.dialog);
		LayoutInflater inflater = LayoutInflater.from(context);
		View layout = inflater.inflate(R.layout.multi_dialog, null);
		dialog.setContentView(layout);
		TextView txtdialog_title = (TextView) layout
				.findViewById(R.id.txtdialog_title);
		txtdialog_title.setText(title);
		TextView txtdialogMessage = (TextView) layout
				.findViewById(R.id.txtdialogMessage);
		txtdialogMessage.setText(message);

		btn1 = (Button) layout.findViewById(R.id.btn_btn1);
		btn1.setText(btn1Text);

		btn2 = (Button) layout.findViewById(R.id.btn_btn2);
		btn2.setText(btn2Text);

		btn3 = (Button) layout.findViewById(R.id.btn_btn3);
		btn3.setText(btn3Text);

		btn4 = (Button) layout.findViewById(R.id.btn_btn4);
		btn4.setText(btn4Text);

		btn1.setOnClickListener(btn1_onClickListener);
		btn2.setOnClickListener(btn2_onClickListener);
		btn3.setOnClickListener(btn3_onClickListener);
		btn4.setOnClickListener(btn4_onClickListener);

		TableRow table_confirm = (TableRow) layout
				.findViewById(R.id.table_confirm);
		TableRow table_alert = (TableRow) layout.findViewById(R.id.table_alert);
		TableRow table_FourButton = (TableRow) layout
				.findViewById(R.id.table_FourButton);
		TableRow table_alertPs = (TableRow) layout
				.findViewById(R.id.table_alertPs);

		table_FourButton.setVisibility(View.VISIBLE);
		table_confirm.setVisibility(TableRow.GONE);
		table_alert.setVisibility(TableRow.GONE);
		table_alertPs.setVisibility(TableRow.GONE);
		btn1.setVisibility(View.GONE);
		btn2.setVisibility(View.GONE);
		dialog.show();
	}

	public void MessageDialog() {
		Toast tt = Toast.makeText(context, message, Toast.LENGTH_LONG);
		tt.show();
	}

	public void WaitDialog() {
		dialog = new AlertDialog.Builder(context).create();
		dialog.show();
		dialog.setContentView(R.layout.wait);
		TextView txtmes = (TextView) dialog.findViewById(R.id.textView1);
		txtmes.setText(message);

	}

	public void show() {
		switch (dialogType) {
		case DialogType.MESSAGE:
			MessageDialog();
			break;
		case DialogType.CONFIRM:
			ConfirmDialog();
			break;
		case DialogType.ALERT:
			AlertDialog();
			break;
		case DialogType.WAITING:
			WaitDialog();
			break;
		case DialogType.FourButton:
			FourButtonDialog();
			break;
		case DialogType.DialogPs:
			PsjgDialog();
			break;
		default:
			AlertDialog();
			break;
		}
	}

	public static class DialogType {
		// 登录欢迎界面设置服务器窗口
		public static final int MESSAGE = 1;
		// 提醒窗口
		public static final int ALERT = 2;
		// 病人列表页面退出提示窗口
		public static final int CONFIRM = 4;
		// 等待窗口
		public static final int WAITING = 8;
		// 口服医嘱确认
		public static final int FourButton = 16;
		// 皮试结果输入
		public static final int DialogPs = 32;
	}

	public void cancel() {
		// TODO 自动生成的方法存根
		dialog.cancel();
	}

	public String getPsjg() {
		String strPsjg = "";
		RadioButton rbtPsjg1 = (RadioButton) dialog.findViewById(R.id.rbtPsjg1);
		RadioButton rbtPsjg2 = (RadioButton) dialog.findViewById(R.id.rbtPsjg2);
		if (rbtPsjg1.isChecked())
			strPsjg = "-";
		else if (rbtPsjg2.isChecked())
			strPsjg = "+";
		return strPsjg;
	}

	public String getPsqmgh() {
		String strGh = "";
		EditText edtPsgh = (EditText) dialog.findViewById(R.id.edtPsgh);
		strGh = edtPsgh.getText().toString().trim();
		return strGh;
	}

}
